<template>
  <Card title="编辑临采价格核查">
    <PriceSupervisionForm :dataSource="dataSource" type="temp" ref="price-form" />
    <div class="actions">
      <el-button :loading="loading" :disabled="!inited" @click="handleSubmit()" type="primary">确认提交</el-button>
      <el-button :loading="loading" :disabled="!inited" @click="handleStage()">暂存</el-button>
      <el-button :loading="loading" @click="handleBack()">返回上一层</el-button>
    </div>
  </Card>
</template>
<script>
import { mapState } from 'vuex';
import {
  stagingSupervisionPriceItemService,
  submitSupervisionPriceItemService,
} from '@/api/supplier/supervision';
import {
  getSupervisionTempPriceItemService
} from '@/api/supplier/supervision/approval';
import {
  getTempPriceItem
} from '@/api/supplier/supervision/tempPrice';
import ManageArea from '@/mixins/supplier/manageArea';
import PriceSupervisionForm from '@/pages/supplier/components/priceSupervisionForm'

export default {
  mixins: [ManageArea],
  components: { PriceSupervisionForm },
  data () {
    const { id = 0, processId = 0 } = this.$route.query
    return {
      id,
      processId,
      inited: false,
      loading: false,
      dataSource: null,
    }
  },
  computed: {
    ...mapState('supplier/priceItem', [ 'priceItem' ]),
  },
  created () {
    if (this.processId) {
      if (this.priceItem) {
        this.dataSource = this.priceItem
        this.inited = true
      } else {
        this.getPriceItemByProcessId()
      }
    } else if (this.id) {
      this.getPriceItem()
    } else {
      this.handleBack()
    }
  },
  methods: {
    async getPriceItemByProcessId () {
      const resp = await getSupervisionTempPriceItemService({ id: this.processId })
      if (resp && resp.result) {
        this.dataSource = resp.result
      }
      this.inited = true
    },
    // 获取详情数据
    async getPriceItem () {
      const resp = await getTempPriceItem({ id: this.id })
      console.log('resp', resp)
      if (resp && resp.result) {
        this.dataSource = resp.result
      }
      this.inited = true
    },
    // 提交
    handleSubmit () {
      this.$refs['price-form'].getData(1).then(data => {
        if (data) {
          this.editPriceItem(data).catch((reason) => {
            console.log('reason', reason)
            this.loading = false
          })
        }
      })
    },
    // 暂存
    handleStage () {
      this.$refs['price-form'].getData().then(data => {
        if (data) {
          this.stagePriceItem(data).catch((reason) => {
            console.log('reason', reason)
            this.loading = false
          })
        }
      })
    },
    // 编辑
    async editPriceItem (data) {
      this.loading = true;
      const resp = await submitSupervisionPriceItemService(data)
      this.loading = false;
      if (resp && resp.status === '0') {
        this.$message.success('编辑成功')
        this.handleBack()
      }
    },
    // 暂存
    async stagePriceItem (data) {
      this.loading = true;
      const resp = await stagingSupervisionPriceItemService(data)
      this.loading = false;
      if (resp && resp.status === '0') {
        this.$message.success('保存成功')
        this.handleBack()
      }
    },
    handleBack () {
      this.$router.go(-1)
    },
  }
}
</script>
